package day05;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/*
@Author: 王少杰
@Date: 2025/1/4
@Time: 14:57
@FileName: Demo03.java
@描述:
List: 集合
    java.util中实现的集合类
    Collection 是集合类的父类
    有序的，元素可重复的，可变长度的。
    List接口。实现类:
        ArrayList:可以看做一个可变长度的数组
        LinkedList:用链表的方式实现的List。每个元素还存储上上一个元素后后一个元素的引用。

*/
public class Demo03 {
    public static void main(String[] args) {
        //<Integer> 列表中元素的类型
        List<Integer> scores = new ArrayList<>();
        //添加元素
        scores.add(90);
        scores.add(80);
        scores.add(70);
        scores.add(100);
        scores.add(50);
        System.out.println(scores.size());
        System.out.println(scores);
        //获取元素
        System.out.println("根据索引获取:" + scores.get(0));
        System.out.println("根据索引获取:" + scores.get(4));
        //删除元素
        System.out.println(scores.remove(0));//删除下标/索引删除
        System.out.println(scores);
        System.out.println(scores.remove(Integer.valueOf(100)));//直接删除元素
        //查找
        System.out.println("100在列表中是否存在:" + scores.contains(100));

        //遍历--下标
        for (int i = 0; i < scores.size(); i++) {
            System.out.print(scores.get(i) + ",");
        }
        System.out.println();
        //遍历--for each
        for (Integer score : scores) {
            System.out.print(score + ",");
        }
        System.out.println();
        //迭代器
        Iterator<Integer> iterator = scores.iterator();
        while (iterator.hasNext()) {
            System.out.print(iterator.next() + ",");
        }
        System.out.println();


        List<Student> students = new ArrayList<>();
        Student stu1 = new Student("张飞",20);
        Student stu2 = new Student("关羽",25);
        Student stu3 = new Student("刘备",30);
        students.add(stu1);
        students.add(stu2);
        students.add(stu3);
        //遍历列表，把学生姓名打印到一行，用逗号分隔开
        for (Student stu : students) {
            System.out.print(stu.getName() + ",");
        }
        System.out.println();
        for (int i = 0; i < students.size(); i++) {
            System.out.print(students.get(i).getName() + ",");
        }
        System.out.println();

        Iterator<Student> iterator1 = students.iterator();
        while (iterator1.hasNext()) {
            System.out.print(iterator1.next().getName() + ",");
        }
        System.out.println();

    }
}
//学生类，姓名和年龄两个属性，构造方法，toString方法
//创建一个列表，将3个学生加入列表
class Student{
    String name;
    int age;

    public Student() {
    }

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
